package com.sz.stock.controller;


import com.alibaba.fastjson.JSON;
import com.sz.stock.client.UserClient;
import com.sz.stock.common.PageBean;
import com.sz.stock.common.ResultBean;
import com.sz.stock.common.SmsConfig;
import com.sz.stock.model.Bankcard;
import com.sz.stock.model.Moneytrad;
import com.sz.stock.model.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.Duration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserClient userClient;

    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    public void setUserClient(UserClient userClient) {
        this.userClient = userClient;
    }
    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @RequestMapping("/testConnection")
    public String testConnection() {
        userClient.testConnection();
        return "初始化成功，可以放心进行服务调用操作";
    }

    /**
     * 登录功能；
     * @param map
     * @return
     */
    @RequestMapping("/login")
    public ResultBean login(@RequestBody Map<String,Object> map) {
        TUser tUser = new TUser();
        tUser.setUserphone((String)map.get("userphone"));
        tUser.setUserpwd((String)map.get("userpwd"));
        return userClient.resultLogin(tUser.getUserphone(), tUser.getUserpwd());
    }

    /**
     * 根据userid查询银行卡；
     * @param userid
     * @return
     */
    @RequestMapping("/user/sBankCard")
    public ResultBean searchBankCard(@RequestParam("userid") String userid){
        return userClient.searchBankCard(userid);
    }

    /**
     * 根据userid从redis中取出user的信息
     * @param userid
     * @return
     */
    @RequestMapping("/getUsrMsg")
    public ResultBean getUserByUid(@RequestParam("userid")String userid){
        String redis_user_object = "stock_user_object_userid";
        redis_user_object = redis_user_object.replace("userid",userid);
        TUser tUser = JSON.parseObject(stringRedisTemplate.boundValueOps(redis_user_object).get(),TUser.class);
        return new ResultBean(202,"从redis中获取user信息",tUser);
    }

    @RequestMapping("/user/register")
    public ResultBean regUser(@RequestBody TUser user , @RequestParam(value = "smsCode") String smsCode) {
        String key = "PhoneCode"+user.getUserphone();
        String code = stringRedisTemplate.opsForValue().get(key);
        if (code != null && code.equals(smsCode)) {
            return userClient.register(user);
        } else {
            return new ResultBean(-1, "验证码有误!", null);
        }
    }

    //2.用户关联开户账户
    @RequestMapping("/user/openAccount")
    public ResultBean openAccount(@RequestBody TUser user , @RequestParam(value = "bankCard") String bankCard) {
        if (user.getUserid() == null || "null".equals(user.getUserid().toString()) || "undefined".equals(user.getUserid().toString())) {
            return new ResultBean(-1, "检查是否登录", null);
        }
        ResultBean resultBean = userClient.openAccount(user, bankCard);
        return resultBean ;
    }
//    /**
//     * 发送验证码
//     */
//    @GetMapping("/user/sendCode")
//    public String sendCode(String userphone) throws Exception {
//        String code = SmsConfig.yt(userphone);
//        userClient.sendCode(userphone);
//        stringRedisTemplate.opsForValue().set("code", code, Duration.ofMinutes(1));
//        return code;
//    }

    /**
     * 重置密码
     * @param userphone
     * @param userupwd
     * @param usercode
     * @return
     */
    @GetMapping(value = "/user/resetPassword")
    public ResultBean register(String userphone,String userupwd, String usercode) {
        ResultBean tc=userClient.resetPassword(userphone,userupwd,usercode);
        if (tc.getCode()==200){
            return new ResultBean(200,"重置密码成功!",tc);
        }else {
            return new ResultBean(555, "验证码错误,请重新输入!!!!", tc);
        }
    }
    /**
     * 判断开户验证码是否正确
     */
    @RequestMapping(value = "user/openCode", method = RequestMethod.GET)
    @ResponseBody
    public Boolean openCode(@RequestParam("code") String code) {
       //String redisCode = stringRedisTemplate.opsForValue().get("code");
        String redisCode="123456";
        userClient.openCode(code);
        return redisCode != null && redisCode.equals(code);
    }

    /**
     * 根据userid查询所有用户
     * @param userid
     * @return
     */
    @RequestMapping(value = "user/usermoney")
    public List<TUser> getAllmoney(@RequestParam("userid") int userid){
        return userClient.getAllmoney(userid);
    }

    /**
     * 充值
     * @param userid
     * @param balance
     * @return
     */
    @RequestMapping("user/topup")
    public ResultBean topup(@RequestParam("userid") int userid,@RequestParam("balance") BigDecimal balance){
        ResultBean cz=userClient.topup(userid,balance);
        if (cz.getCode()==0){
            return new ResultBean(0,"充值成功",cz);
        }else {
            return new ResultBean(204,"充值失败",cz);
        }
    }

    /**
     * 提现
     * @param userid
     * @param balance
     * @return
     */
    @RequestMapping("user/withdraw")
    public ResultBean withdraw(@RequestParam("userid") int userid,@RequestParam("balance") BigDecimal balance){
        ResultBean cz=userClient.withdraw(userid,balance);
        if (cz.getCode()==0){
            return new ResultBean(0,"提现成功",cz);
        }else {
            return new ResultBean(204,"提现失败",cz);
        }
    }

    /**
     * 根据原密码修改密码
     * @param userid
     * @param olduserpwd
     * @param newuserpwd
     * @return
     */
    @RequestMapping(value = "/user/modification")
    public ResultBean Modification(int userid,String olduserpwd,String newuserpwd){
        ResultBean n=userClient.Modification(userid,olduserpwd,newuserpwd);
        if (n.getCode()==200) {
            return new ResultBean(200,"修改成功!",n);
        }else {
            return new ResultBean(400,"修改失败!",n);
        }
    }

    /**
     * 修改用户名
     * @param userid
     * @param username
     * @return
     */
    @RequestMapping(value = "/user/modifyname")
    public ResultBean ModifName(int userid,String username){
        ResultBean cs=userClient.ModifName(userid,username);
        if (cs.getCode()==200) {
            return new ResultBean(200,"修改成功!",cs);
        }else {
            return new ResultBean(400,"修改失败!",cs);
        }
    }

    /**
     * 充值记录
     * 根据查询一个用户来判断该用户的充值记录
     * @param userid
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/user/list")
    public PageBean<Moneytrad> getAllCarByid(int userid, int pageIndex, int pageSize){
        return userClient.getAllCarByid(userid,pageIndex,pageSize);
    }
}
